package com.xuxin.baby.model;

import com.blankj.utilcode.util.LogUtils;
import com.xuxin.baby.base.BaseConstant;
import com.xuxin.baby.bean.BannerBean;
import com.xuxin.baby.bean.BaseBean;
import com.xuxin.baby.bean.ChildGreatBean;
import com.xuxin.baby.bean.DoctorBean;
import com.xuxin.baby.bean.ReaderVideoBean;
import com.xuxin.baby.bean.UserChildBean;
import com.xuxin.baby.contract.MainContract;
import com.xuxin.baby.network.Retrofit;
import com.xuxin.baby.network.RetrofitApi;
import com.xuxin.baby.network.myObserver;
import com.xuxin.baby.presenter.MainPresenter;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.observers.DisposableObserver;
import io.reactivex.rxjava3.schedulers.Schedulers;

/**
 * Created by xu_xin
 * Created on 2020/10/01 0022
 * 首页
 */
public class MainModel implements MainContract.Model {
    @Override
    public void getUserChild(MainPresenter presenter, String tenantId, String token, String memberId) {
        Retrofit.getInstance().createReq(RetrofitApi.class)
                .getUserChild(tenantId, token, "1",memberId, 0, BaseConstant.page_limit)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new myObserver<UserChildBean>() {
                    @Override
                    public void onNextX(@NonNull UserChildBean baseBean) {
                        if (baseBean.isSuccess()) {
                            presenter.onSuccess(baseBean);
                        } else {
                            presenter.onError(baseBean);
                        }
                    }
                    @Override
                    public void onError(BaseBean baseBean) {
                        presenter.onError(baseBean);
                    }
                    @Override
                    public void onError(@NonNull Throwable e) {
                        presenter.onError(null);
                    }

                    @Override
                    public void onComplete() {
                        //presenter.onComplete();
                    }
                });
    }

    @Override
    public void getBanner(MainPresenter presenter, String tenantId, String token) {
        Retrofit.getInstance().createReq(RetrofitApi.class)
                .getBanner(tenantId, token,"1")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new myObserver<BannerBean>() {
                    @Override
                    public void onNextX(@NonNull BannerBean baseBean) {
                        if (baseBean.isSuccess()) {
                            presenter.onSuccess(baseBean);
                        } else {
                            presenter.onError(baseBean);
                        }
                    }
                    @Override
                    public void onError(BaseBean baseBean) {
                        presenter.onError(baseBean);
                    }
                    @Override
                    public void onError(@NonNull Throwable e) {
                        presenter.onError(null);
                    }

                    @Override
                    public void onComplete() {
                      //  presenter.onComplete();
                    }
                });
    }

    @Override
    public void getChildGreat(MainPresenter presenter, String tenantId, String token, String childId, String eventType, int skipCount) {
        Retrofit.getInstance().createReq(RetrofitApi.class)
                .getChildGreat(tenantId, token,"1", childId, eventType, skipCount * BaseConstant.page_limit, BaseConstant.page_limit)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new myObserver<ChildGreatBean>() {
                    @Override
                    public void onNextX(@NonNull ChildGreatBean childGreatBean) {
                        if (childGreatBean.isSuccess()) {
                            presenter.onSuccess(childGreatBean);
                        } else {
                            presenter.onError(childGreatBean);
                        }
                    }
                    @Override
                    public void onError(BaseBean baseBean) {
                        presenter.onError(baseBean);
                    }
                    @Override
                    public void onError(@NonNull Throwable e) {
                        presenter.onError(null);
                    }

                    @Override
                    public void onComplete() {
                        //presenter.onComplete();
                    }
                });
    }

    @Override
    public void getTips(MainPresenter presenter, String tenantId, String token, String categoryId) {
        Retrofit.getInstance().createReq(RetrofitApi.class)
                .getReaderVideo(tenantId,
                        token,
                        "1",
                        categoryId,
                        null,
                        null,
                        "common",
                        null,
                        0,
                        10)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new myObserver<ReaderVideoBean>() {
                    @Override
                    public void onNextX(@NonNull ReaderVideoBean readerVideoBean) {
                        if (readerVideoBean.isSuccess()) {
                            presenter.onSuccessTips(readerVideoBean);
                        } else {
                            presenter.onError(readerVideoBean);
                        }
                    }
                    @Override
                    public void onError(BaseBean baseBean) {
                        presenter.onError(baseBean);
                    }
                    @Override
                    public void onError(@NonNull Throwable e) {
                        presenter.onError(null);
                    }

                    @Override
                    public void onComplete() {
                        //presenter.onComplete();
                    }
                });
    }

    @Override
    public void getVideo(MainPresenter presenter, String tenantId, String token, String categoryId, String ageRge) {
        Retrofit.getInstance().createReq(RetrofitApi.class)
                .getReaderVideo(tenantId,
                        token,
                        "1",
                        categoryId,
                        null,
                        null,
                        "video",
                        ageRge,
                        0,
                        2)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new myObserver<ReaderVideoBean>() {
                    @Override
                    public void onNextX(@NonNull ReaderVideoBean readerVideoBean) {
                        if (readerVideoBean.isSuccess()) {
                            presenter.onSuccessVideo(readerVideoBean);
                        } else {
                            presenter.onError(readerVideoBean);
                        }
                    }
                    @Override
                    public void onError(BaseBean baseBean) {
                        presenter.onError(baseBean);
                    }
                    @Override
                    public void onError(@NonNull Throwable e) {
                        presenter.onError(null);
                    }

                    @Override
                    public void onComplete() {
                        //presenter.onComplete();
                    }
                });
    }

    @Override
    public void getReader(MainPresenter presenter, String tenantId, String token, String categoryId, String ageRge) {
        Retrofit.getInstance().createReq(RetrofitApi.class)
                .getReaderVideo(tenantId,
                        token,
                        "1",
                        categoryId,
                        null,
                        null,
                        "common",
                        ageRge,
                        0,
                        2)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new myObserver<ReaderVideoBean>() {
                    @Override
                    public void onNextX(@NonNull ReaderVideoBean readerVideoBean) {
                        if (readerVideoBean.isSuccess()) {
                            presenter.onSuccessReader(readerVideoBean);
                        } else {
                            presenter.onError(readerVideoBean);
                        }
                    }
                    @Override
                    public void onError(BaseBean baseBean) {
                        presenter.onError(baseBean);
                    }
                    @Override
                    public void onError(@NonNull Throwable e) {
                        presenter.onError(null);
                    }

                    @Override
                    public void onComplete() {
                        //presenter.onComplete();
                    }
                });
    }

    @Override
    public void getDoctor(MainPresenter presenter, String tenantId, String token, int skipCount) {
        Retrofit.getInstance().createReq(RetrofitApi.class)
                .getDoctor(tenantId, token, "1",skipCount * BaseConstant.page_limit, BaseConstant.page_limit)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new myObserver<DoctorBean>() {
                    @Override
                    public void onNextX(@NonNull DoctorBean doctorBean) {
                        if (doctorBean.isSuccess()) {
                            presenter.onSuccess(doctorBean);
                        } else {
                            presenter.onError(doctorBean);
                        }
                    }
                    @Override
                    public void onError(BaseBean baseBean) {
                        presenter.onError(baseBean);
                    }
                    @Override
                    public void onError(@NonNull Throwable e) {
                        presenter.onError(null);
                    }

                    @Override
                    public void onComplete() {
                        presenter.onComplete();
                    }
                });
    }

    @Override
    public void updateCity(MainPresenter presenter, String tenantId, String token, String memberId, String city) {
        Retrofit.getInstance().createReq(RetrofitApi.class)
                .updateCity(tenantId, token, "1",memberId, city)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new myObserver<BaseBean>() {
                    @Override
                    public void onNextX(@NonNull BaseBean baseBean) {
                        if (baseBean.isSuccess()) {
                            LogUtils.e("更新定位成功");
                        } else {
                            LogUtils.e("更新定位失败");
                        }
                    }
                    @Override
                    public void onError(BaseBean baseBean) {
                        presenter.onError(baseBean);
                    }
                    @Override
                    public void onError(@NonNull Throwable e) {
                    }

                    @Override
                    public void onComplete() {
                    }
                });
    }
}